package protocol.transactions;

import java.lang.reflect.Type;
import java.util.List;

import protocol.XoneProtocol;
import protocol.XoneServiceCode;
import protocol.meta.Source;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.netease.http.THttpRequest;

import db.manager.ManagerDiscussTip;

public class TransactionSourceInputTipList extends XoneBaseTransaction {
    private String mType = null;
    
    public TransactionSourceInputTipList(String type) {
        super(TRANSACTION_TYPE_SOURCEINPUTTIPLIST);
        this.mType = type;
    }

    @Override
    protected void onXoneTransactionSuccess(int code, Object obj) {
        if (obj instanceof JsonElement) {
            try {
                JsonElement jsonElement = ((JsonElement)obj).getAsJsonObject().get("list");
                Gson gson = new Gson();
                List<Source> list = null;
                Type collectionType = new TypeToken<List<Source>>(){}.getType();
                list = gson.fromJson(jsonElement, collectionType);
                ManagerDiscussTip.replaceSource(list, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS, null);
    }

    @Override
    public void onTransact() {
        THttpRequest request = XoneProtocol.getInstance().createSourceInputTipListRequest(mType);
        sendRequest(request);
    }
}
